const app = getApp();
Page({

  /**
   * 页面的初始数据
   */
  data: {
    currenTuijianIndex: 0,
    currentMID: 81,
    FjLoginLog: wx.getStorageSync('FjLoginLog'),
    baojiaTopImg: [],
    baojiaNav: [],
    pic: [],
    CJshow: false,
    classname: '',
    provinceIndex: 0,
    cityIndex: 0,
    CaiJiUserTips: '',
    CJcomType: ['废金属', '再生塑料', '废纸', '铜厂、铜材厂', '调剂商、贸易商', '钢厂、钢铁冶金'],
    CJcomTypeIndex: 0,
    CaiJiUserTips: '',
    liuyantext: '',
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: async function (options) {
    wx.showLoading({
      title: '加载中',
    })
    await this.queryFeijiu_BJ_Get_Index() //

  },
  toList: function (e) {
    wx.navigateTo({
      url: 'bjlist?classid=' + e.currentTarget.dataset.classid + '&type=' + e.currentTarget.dataset.type + '&titname=' + e.currentTarget.dataset.titname
    })
  },
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {
    // this.queryisCJ()
  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {
    var that = this;
    var shareObj = {
      title: this.data.info.Title,
      path: '/packageA/pages/index/index',
      imgUrl: '',
    }
    return shareObj;
  },
  async navJumpDetail(e) {
    // console.log(e, '跳转二级')
    let ClassIndex = e.currentTarget.dataset.classindex,
      type = e.currentTarget.dataset.type
    if (type == 2) { //发布报价
      await this.queryisCJ()
    } else if (type == 3) {
      wx.switchTab({
        url: '/pages/dingyue/index',
      })
    } else if (type == 4) {
      wx.showToast({
        title: '请前往Feijiu网APP查看详情',
        icon: 'none'
      })
    } else {
      wx.navigateTo({
        url: '/packageA/pages/index/baojiaNavDetal/baojiaNavDetail?ClassIndex=' + ClassIndex,
      })
    }
  },
  queryFeijiu_BJ_Get_Index(e) {
    let LoginUID,
      that = this,
      baojiaTopImg,
      baojiaNav,
      icon,
      baojiaqushi,
      wodedingyue,
      shangjituijian,
      quanweiguandian,
      pic,
      shishiqushi,
      feigangqushi,
      feitongqushi,
      feiluqushi,
      lijidingyue,
      dingyueList,
      jiucuoFabu
    if (app.checkFjLogin()) { //token先看是不是登录了
      LoginUID = that.data.FjLoginLog.UID
    }
    // console.log(LoginUID, 'uid')
    app.getDaoPost('api/FeijiuApp/Feijiu_BJ_Get_Index', {
      LoginUID: LoginUID
    }, r => {
      // console.log(r, '报价')

      if (r.code == 200) {
        r.data.forEach((item, index) => {
          if (item.MID == 1) { //Banner
            baojiaTopImg = item.MData,
              baojiaNav = item.MData
            that.setData({
              baojiaTopImg,
              baojiaNav
            })
          } else if (item.MID == 2) { //图标
            icon = item.MData
            that.setData({
              icon
            })
          } else if (item.MID == 3) { //废钢趋势
            item.MData.forEach((item, index) => {
              let status = item.Qdownup.slice(0, 1)
              if (status == '+') {
                item.status = 'up'
              } else if (status == '-') {
                if (item.Qdownup.length > 1) {
                  item.status = 'down'
                } else {
                  item.status = ''
                }
              } else if (status == '0') {
                item.status = 'peace'
              } else if (parseInt(status) > 0) {
                item.status = 'up'
                item.Qdownup = '+' + item.Qdownup
              }
            })
            feigangqushi = item
            that.setData({
              feigangqushi
            })
          } else if (item.MID == 4) { //废钢趋势
            item.MData.forEach((item, index) => {
              let status = item.Qdownup.slice(0, 1)
              if (status == '+') {
                item.status = 'up'
              } else if (status == '-') {
                item.status = 'down'
              } else {
                item.status = 'peace'
              }
            })
            feitongqushi = item
            that.setData({
              feitongqushi
            })
          } else if (item.MID == 5) { //废铝趋势
            item.MData.forEach((item, index) => {
              let status = item.Qdownup.slice(0, 1)
              if (status == '+') {
                item.status = 'up'
              } else if (status == '-') {
                item.status = 'down'
              } else {
                item.status = 'peace'
              }
            })
            feiluqushi = item
          } else if (item.MID == 6) { //立即订阅
            lijidingyue = item.MData
          } else if (item.MID == 11) { //pic图片
            // console.log(item.MData,'777')
            pic = item.MData
            that.setData({
              pic
            })
          } else if (item.MID == 8) { //商机推荐
            shangjituijian = item
            that.setData({
              tuijianMUpTime: item.MData[0].MUpTime
            })
          } else if (item.MID == 7) { //我的订阅
            dingyueList = item
          } else if (item.MID == 9) { //权威观点
            quanweiguandian = item
          } else if (item.MID == 10) { //我要纠错 我要发布
            jiucuoFabu = item
          }
        })
        that.setData({
          // baojiaTopImg,
          // baojiaNav,
          // icon,
          baojiaqushi,
          shangjituijian,
          quanweiguandian,
          // pic,
          shishiqushi,
          // feigangqushi,
          // feitongqushi,
          feiluqushi,
          lijidingyue,
          dingyueList,
          jiucuoFabu
        })
        wx.hideLoading({
          success: (res) => { },
        })
      }
    })
  },
  stopTouchMove(e) {
    // console.log(e, '滑动')
    if (this.data.baojiaTopImg.length <= 0) {
      return false
    }
  },
  async jumpFabu() {
    await this.queryisCJ()

  },
  // 商机推荐标题点击
  cilckSahngjiTuijian(e) {
    let index = e.currentTarget.dataset.index,
      id = e.currentTarget.dataset.id,
      that = this,
      arr = that.data.shangjituijian
    that.setData({
      currenTuijianIndex: index,
      currentMID: id,
      tuijianMUpTime: arr.MData[index].MUpTime
    })
  },
  zoushiORzhishu(e) {
    wx.showToast({
      title: '请前往Feijiu网APP查看详情',
      icon: 'none'
    })
  },
  stopTouchmove2() {

    if (this.data.lijidingyue.length <= 1) {
      return false
    }
  },
  jumpdingyue() {
    wx.navigateTo({
      url: '/pages/dingyue/keylist',
    })
    wx.switchTab({
      url: '/pages/dingyue/index',
    })
  },
  async bottomPic(e) {
    // console.log(e, '底部图片')
    let id = e.currentTarget.dataset.id
    if (id == 1) { //我要纠错
      wx.navigateTo({
        url: '/packageA/pages/index/baojiamyjiucuo/baojiamyjiucuo',
      })
    } else if (id == 2) { //我要发布
      await this.queryisCJ()
      // if (this.data.isCaiJiUser == 1) {
      //   wx.navigateTo({
      //     url: '/packageA/pages/index/baojiaFaubu/baojiaFabu',
      //   })
      // }
    }
  },

  // 申请成为采集单位
  applyCJ() {
    let that = this
    // LoginUID 登录用户编号     CompanyType 公司类型名称    Message 留言   CompanyName 公司名  LinkMan 联系人姓名   LinkTel 联系人电话
    if (!this.data.Linkman) {
      wx.showToast({
        title: '请填写联系人',
        icon: 'none'
      })
      return
    } else if (!this.data.Mobile) {
      wx.showToast({
        title: '请填写联系方式',
        icon: 'none'
      })
      return
    } else if (this.data.Mobile.length < 11) {
      wx.showToast({
        title: '手机号格式不正确',
        icon: 'none'
      })
      return
    }
    let input = {
      LoginUID: wx.getStorageSync('FjLoginLog').UID,
      CompanyType: this.data.CJcomType[this.data.CJcomTypeIndex],
      Message: this.data.liuyantext,
      CompanyName: this.data.ComName ? this.data.ComName : '',
      LinkMan: this.data.Linkman,
      LinkTel: this.data.Mobile
    }
    app.getDaoPost('api/FeijiuApp/Feijiu_BJ_apply_CollectUser', input, r => {
      // console.log(r, '采集单位申请')
      if (r.code == 200) {
        wx.showToast({
          title: r.data,
          icon: 'none'
        })
        that.setData({
          CJshow: false
        })
      } else {
        wx.showToast({
          title: r.message,
          icon: 'none'
        })
      }
    })
  },
  //查询是不是采集单位 有没有登录
  async queryisCJ() {
    let that = this
    if (app.checkFjLogin()) {
      var FjLoginLog = wx.getStorageSync('FjLoginLog');
      var api_input = {
        uid: FjLoginLog.UID,
        upwd: FjLoginLog.UPwd
      }
      //采集单位
      await app.getDaoPost('api/FeijiuApp/Post_FJ_UserInfoByUID', api_input, function (r) {
        var udata = JSON.parse(app.aesDec(r.data));
        // console.log(udata, '===采集单位');
        that.setData({
          ComName: udata.Uinfo[0].ComName,
          Linkman: udata.Uinfo[0].Linkman,
          Mobile: udata.Uinfo[0].Mobile,
          isCaiJiUser: udata.isCaiJiUser,
          CaiJiUserTips: udata.CaiJiUserTips
        })
        if (udata.isCaiJiUser == 0) { //1 是 0不是没有提交申请 2不是提交过
          that.setData({
            CJshow: true,
          })
        } else if (udata.isCaiJiUser == 2) { //
          wx.showToast({
            title: udata.CaiJiUserTips,
            icon: 'none'
          })

        } else if (udata.isCaiJiUser == 1) {
          wx.navigateTo({
            // url: '/packageA/pages/index/baojiaFaubu/baojiaFabu',
            url: '/packageB/pages/mybaojia/index'
          })
        }

      })
    } else {
      wx.navigateTo({
        url: '/pages/login/index'
      });
    }
  },
  onCloseCJ() {
    this.setData({
      CJshow: false
    })
  },
  selectComType(e) {
    let index = e.currentTarget.dataset.index
    this.setData({
      CJcomTypeIndex: index
    })
  },
  GJcomInput(e) {
    this.setData({
      ComName: e.detail.value
    })
  },
  GJUserPhoneInput(e) {
    this.setData({
      Mobile: e.detail.value
    })
  },
  GJUserNameInput(e) {
    this.setData({
      Linkman: e.detail.value
    })
  },
  liuyanInput(e) {
    this.setData({
      liuyantext: e.detail.value
    })
  },
  callPhone(e) {
    let ph = e.currentTarget.dataset.ph
    wx.makePhoneCall({
      phoneNumber: ph //仅为示例，并非真实的电话号码
    })
  },
  onClickMOre(e) {
    if (this.data.currenTuijianIndex == 1) {
      wx.navigateTo({
        url: '/packageA/pages/zichanchuzhi/index',
      })
    } else if (this.data.currenTuijianIndex == 0) {
      wx.switchTab({
        url: '/pages/zhaobiaogonggao/index',
      })
    } else if (this.data.currenTuijianIndex == 2 || this.data.currenTuijianIndex == 3) {
      wx.navigateTo({
        url: '/pages/gongqiuxinxi/index',
      })
    }
  },
  zichanchuzhiDetail(e) { //资产处置详情跳转
    // console.log(e, '===')
    let id = e.currentTarget.dataset.id,
      uuid = e.currentTarget.dataset.uuid

    if (this.data.currenTuijianIndex == 1) {
      wx.navigateTo({
        url: '/packageA/pages/zichanchuzhi/detailed?gqid=' + id,
      })
    } else if (this.data.currenTuijianIndex == 0) {
      wx.navigateTo({
        url: '/pages/zhaobiaogonggao/detailed?uuid=' + uuid,
      })
    } else if (this.data.currenTuijianIndex == 2 || this.data.currenTuijianIndex == 3) {
      wx.navigateTo({
        url: '/pages/gongqiuxinxi/detailed?gqid=' + id,
      })
    }


  },
  toCompany: function (e) {

    let item = e.currentTarget.dataset.item
    let loginuid = wx.getStorageSync('FjLoginLog').UID
    let parame = {
      lytype: 3,
      aid: item.Aid ? item.Aid: '',
      loginuid: loginuid ? loginuid : "0"
    }
    // 如果是轮播图 ， 那就添加埋点
    this.maiDianData(parame) 
    console.log('-----', e.currentTarget.dataset.url)

    if (e.currentTarget.dataset.uid != 0) {
      wx.navigateTo({
        url: '/pages/gongqiuxinxi/shangpu?longUID=' + e.currentTarget.dataset.uid
      })
    } else {
      var url = e.currentTarget.dataset.url;
      if (url.substring(0, 20) == 'https://m.feijiu.net' || url.substring(0, 19) == 'http://m.feijiu.net' || url.substring(0, 19) == 'https://s.jupai.net' || url.indexOf('https://wap.feijiu.net') >= 0 ||  url.indexOf('https://m.jupai.net') >= 0 || url.indexOf('http://10.10.10.232') >= 0) {
        // wx.navigateTo({
        //   url: '../other/web?url=' + url
        // })
        wx.navigateTo({
          url: '../other/web',
          success: function (res) {
            // 通过 eventChannel 向被打开页面传送数据
            res.eventChannel.emit('acceptDataFromOpenerPage', {
              url: url
            })
          }
        })
      }else if(!url) {
        // 此时不提示，也不做任何操作
      } else {
        wx.showToast({
          title: '此功能仅限app使用',
          icon: 'none'
        })
      }
    }
  },

  // 点击添加广告的
  maiDianData(parame) {
    app.getDaoPost('api/FeijiuApp/Feijiu_AddADHit_v2024', parame, r => {
    })
  },
})